package com.swordget.controller;

import com.swordget.entity.ColumnDesign;
import com.swordget.entity.TableDesign;
import com.swordget.entity.vo.*;
import com.swordget.exception.VerifyFailException;
import com.swordget.service.TableDefinitionService;
import com.swordget.service.TableManagerService;
import com.swordget.service.facade.TableManagerFacadeService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.IOException;
import java.util.List;


/**
 * @Author: David
 * @Date: Created in 14:13 2019/12/16
 * @Description: 数据表管理
 * @Modified By:
 * @Version:
 */
@RestController
@RequestMapping(path = "/tableManage")
@Slf4j
public class TableManagerCot {
    private final static Logger LOG = LoggerFactory.getLogger(TableManagerCot.class);

    private TableManagerService tableManagerService;

    @Autowired
    private TableDefinitionService tableDefinitionService;

    @Autowired
    private TableManagerFacadeService tableManagerFacadeService;


    @Autowired
    public TableManagerCot(TableManagerService tableManagerService, TableDefinitionService tableDefinitionService) {
        this.tableManagerService = tableManagerService;
        this.tableDefinitionService = tableDefinitionService;
    }

    /**
     * 创建表
     *
     * @param requestVo 前台参数
     * @return
     */
    @PostMapping
    public SimpleResponseVo makeTable(@RequestBody SimpleRequestVo<TableRequestVo> requestVo) {
        try {
            String loginId = requestVo.getLoginId();
            TableRequestVo tableRequestVo = requestVo.getData();
            TableDesign tableDesign = tableRequestVo.getTable();
            List<ColumnDesign> columns = tableRequestVo.getColumns();
            //每个数据库创建表语句不尽相同，这里使用不同的接口进行实现
            //boolean isSuccess = tableManagerService.makeTable(tableDesign, columns, loginId);
            boolean isSuccess = tableManagerFacadeService.makeTable(tableDesign, columns, loginId);
            if (isSuccess) {
                return SimpleResponseVo.success();
            }
        } catch (Exception e) {
            LOG.error("程序执行过程中出现异常", e);
            return SimpleResponseVo.fail("处理失败：", e.getMessage());
        }
        return SimpleResponseVo.fail("处理失败");
    }

    @PostMapping("/editTable")
    public SimpleResponseVo editTable(@RequestBody SimpleRequestVo<TableRequestVo> requestVo) {
        try {
            String loginId = requestVo.getLoginId();
            TableRequestVo tableRequestVo = requestVo.getData();
            TableDesign tableDesign = tableRequestVo.getTable();
            //每个数据库创建表语句不尽相同，这里使用不同的接口进行实现
            boolean isSuccess = tableManagerFacadeService.editTable(tableDesign, loginId);
            if (isSuccess) {
                return SimpleResponseVo.success();
            }
        } catch (Exception e) {
            LOG.error("程序执行过程中出现异常", e);
            return SimpleResponseVo.fail("处理失败：", e.getMessage());
        }
        return SimpleResponseVo.fail("处理失败");
    }

    /**
     * 删除表
     *
     * @param requestVo 前台参数
     * @return
     */
    @DeleteMapping
    public SimpleResponseVo dropTable(@RequestBody SimpleRequestVo<TableRequestVo> requestVo) {
        TableDesign design = requestVo.getData().getTable();
        boolean f = false;
        f = tableManagerService.dropTable(design);
        if (f) {
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");
    }

    /**
     * 修改表结构
     *
     * @param requestVo 前台参数
     * @returnmodifyTable
     */
    @PutMapping
    public SimpleResponseVo modifyTable(@RequestBody SimpleRequestVo<TableRequestVo> requestVo) {
        TableDesign tableDesign = requestVo.getData().getTable();
        List<ColumnDesign> columnDesigns = requestVo.getData().getColumns();
        String loginId = requestVo.getLoginId();
        //boolean isModify = tableManagerService.modifyTable(tableDesign, columnDesigns, loginId);
        boolean isModify = false;
        try {
            isModify = tableManagerFacadeService.modifyTable(tableDesign, columnDesigns, loginId);
        } catch (Exception e) {
            log.error("修改表结构失败,", e);
            return SimpleResponseVo.fail(e.getMessage());
        }
        if (isModify) {
            return SimpleResponseVo.success();
        }
        return SimpleResponseVo.fail("处理失败");
    }


    @GetMapping
    public SimplePageResponseVo getData(SimplePageRequestVo pageVo) {
        return tableManagerService.getTableDesign(pageVo);
    }


    @GetMapping("/column")
    public SimplePageResponseVo getTableData(SimplePageRequestVo pageVo) {
        return tableManagerService.getColumnDesign(pageVo);
    }

    @PostMapping("/checkFile")
    public SimpleResponseVo uploadCheckFile(MultipartHttpServletRequest request) throws IOException {
        SimpleResponseVo responseVo = SimpleResponseVo.fail("处理失败!");
        try {
            MultipartFile multipartFile = request.getFile("uploadFile");
            String tableId = request.getParameter("tableId");
            String loginId = request.getParameter("loginId");
            boolean isUpload = tableManagerService.uploadCheckFile(multipartFile, tableId, loginId);
            if (isUpload) {
                responseVo = SimpleResponseVo.success();
            }
        } catch (Exception e) {
            LOG.error("程序执行过程中出现异常", e);
        }
        return responseVo;
    }


    @GetMapping("/tableMetadata")
    public SimpleResponseVo getTableTableMetadata(String loginId, String tableId) {
        try {
            return SimpleResponseVo.success(tableDefinitionService.getTableComment(loginId, tableId));
        } catch (Exception e) {
            LOG.error("程序执行过程中出现异常", e);
        }
        return SimpleResponseVo.fail("处理失败!");
    }
}
